package com.qqt.csr.im.assembler;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.qqt.csr.im.dto.MsgAccountDTO;
import com.qqt.csr.im.entity.Account;
import com.qqt.csr.im.entity.Room;
import com.qqt.csr.im.entity.Conversation;
import com.qqt.csr.im.dto.ConversationDTO;
import com.qqt.csr.im.vo.resp.RoomRespVO;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public class RoomAssembler {

    public static RoomRespVO convertBy(Room room, Account currentAccount, Map<Long, Account> otherAccountMap){
        List<Account> otherMemberList = Optional.ofNullable(room.getRoomMemberList()).orElseGet(Lists::newArrayList)
                .stream()
                .filter(member -> !member.getAccountId().equals(currentAccount.getId()))
                .map(member -> otherAccountMap.get(member.getAccountId()))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        return RoomRespVO.builder()
                .id(room.getId().toString())
                .member(MsgAccountDTO.of(currentAccount))
                .otherMemberList(MsgAccountDTO.of(otherMemberList))
                .lastConversationId(room.getLastConversationId())
                .lastConversationState(room.getLastConversationState())
                .lastConversationDate(room.getLastConversationDate())
                .name(room.getName())
                .roomExt(room.getExt())
                .build();
    }


    public static RoomRespVO convertBy(Conversation conversation){
        return RoomRespVO.builder()
                .id(conversation.getRoomId())
                .lastConversationId(conversation.getId())
                .lastConversationState(conversation.getState().getCode())
                .lastConversationDate(DateUtil.parse(conversation.getStartTime()))
                .build();
    }

    public static RoomRespVO convertBy(ConversationDTO conversation){
        return RoomRespVO.builder()
                .id(conversation.getRoomId().toString())
                .lastConversationId(conversation.getId())
                .lastConversationState(conversation.getState().getCode())
                .lastConversationDate(conversation.getStartTime())
                .build();
    }
}
